﻿using BonjourQuotation.ObjectModel.AccountData;
using BonjourQuotation.ObjectModel.OptionalData;
using BonjourQuotation.ObjectModel.LoggerData;
using BonjourQuotation.UI.Models.AccountModel;
using BonjourQuotation.UI.Models.OptionalModel;
using BonjourQuotation.UI.Models.PartialModel;
using BonjourQuotation.UI.Models.ReportModel;
using BonjourQuotation.UI.ServiceManagers;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using BonjourQuotation.UI.Models.Shared;


namespace BonjourQuotation.UI.Controllers
{
    public class OptionalController : BaseController
    {
        OptionalManager OptManager;
        private readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        [Authorize]
        [HttpGet]
        public ActionResult ManageOptionals()
        {
            OptManager = new OptionalManager();
            var ManageOptionalsData = OptManager.LoadAllOptionals(0);
            var ManageOptionalsViewModel = InitManageOptionalsSearchParams(ManageOptionalsData);
            this.SetPermission(ManageOptionalsViewModel, AllPermVModel);
            return View("ManageOptionals", ManageOptionalsViewModel);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadOptionalsGrid(ManageOptionalsGridViewModel model)
        {
            OptManager = new OptionalManager();
            ManageOptionalsGridBData ManageOptionalsData = null;
            if (model.IsSearch)
            {
                ManageOptionalsGridBData SearchOptionalsData = new ManageOptionalsGridBData();
                SearchOptionalsData = InitSearchOptional(model);
                ManageOptionalsData = OptManager.LoadSearchedOptionals(SearchOptionalsData);
            }
            else
                ManageOptionalsData = OptManager.LoadAllOptionals(model.PageNum);
            var ManageOptionalsViewModel = InitManageOptionalsVModel(ManageOptionalsData, model.PageNum);
            ManageOptionalsViewModel.TopScrollPossition = model.TopScrollPossition;
            this.SetPermission(ManageOptionalsViewModel, AllPermVModel);
            return PartialView("LoadOptionalsGrid", ManageOptionalsViewModel);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult LoadOptionalDetails(int OptionalID)
        {
            OptManager = new OptionalManager();
            var OptionalDetData = OptManager.LoadOptionalDetailsByID(OptionalID);
            ModelState.Clear();
            var OptionalDetViewModel = InitOptionalDetailsViewModel(OptionalDetData);
            this.SetPermission(OptionalDetViewModel, AllPermVModel);
            if (OptionalID == 0)
                OptionalDetViewModel.Active = true;
            return PartialView("LoadOptionalDetailsGrid", OptionalDetViewModel);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveOptionalDetailData(OptionalDetailsViewModel model)
        {
            OptManager = new OptionalManager();
            var OptionalData = InitOptionalDetailsBData(model);
            if (model.OptionalID == 0)
            {
                var CreateError = OptManager.CreateOptional(OptionalData);
                MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
                {
                    Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
                };
                return PartialView("MessageBox", MessBoxforCreate);
            }
            var EditError = OptManager.SaveOptionalDetailsData(OptionalData);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewOptionalDetail(OptionalDetailsViewModel model)
        {
            OptManager = new OptionalManager();
            var OptionalDetail = InitOptionalDetailsAddBData(model);
            var CreateError = OptManager.CreateOptionalDetailData(OptionalDetail);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message,
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveOptionalGridData(ManageOptionalsGridViewModel model)
        {
            OptManager = new OptionalManager();
            var OptionalData = InitManageOptionalsGridBData(model);
            var EditError = OptManager.SaveOptionalsGridData(OptionalData);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        public PartialViewResult AutoComplete(string PropName, string keyword)
        {
            QuotationManager QuotaManager = new QuotationManager();
            var data = QuotaManager.FindKeywordInEntity(PropName, keyword);
            AutoCompleteViewModel AutoCompVMode = new AutoCompleteViewModel() { Items = data };
            return PartialView("QuotaAutoComplete", AutoCompVMode);
        }
        #region private methods
        private OptionalDetailsBData InitOptionalDetailsAddBData(OptionalDetailsViewModel data)
        {
            OptionalDetailsBData OptionalDetails = new OptionalDetailsBData();
            OptionalDetails.Price = Convert.ToDecimal(data.Price);
            OptionalDetails.PaxFrom = Convert.ToInt16(data.PaxFrom);
            OptionalDetails.PaxTo = Convert.ToInt16(data.PaxTo);
            OptionalDetails.OptionalID = data.OptionalID;
            OptionalDetails.SelectedCurrency = data.SelectedCurrency == 2 ? "CAD" : "USD";
            OptionalDetails.BusIncluded = data.BusIncluded;
            OptionalDetails.GuideIncluded = data.GuideIncluded;
            return OptionalDetails;
        }
        private ManageOptionalsGridBData InitManageOptionalsGridBData(ManageOptionalsGridViewModel data)
        {
            ManageOptionalsGridBData ManOptionalsBData = new ManageOptionalsGridBData();
            ManOptionalsBData.Rows = new List<ManageOptionalsGridRowBData>();
            foreach (var Optional in data.Rows)
                ManOptionalsBData.Rows.Add(new ManageOptionalsGridRowBData
                {
                    OptionalID = Optional.OptionalID,
                    Active = Optional.IsActive,
                });
            return ManOptionalsBData;
        }
        private ManageOptionalsGridViewModel InitManageOptionalsVModel(ManageOptionalsGridBData ManageOptionalsData, int PageNum)
        {
            ManageOptionalsGridViewModel ManOptionalsVModel = new ManageOptionalsGridViewModel();
            ManOptionalsVModel.Rows = new List<ManageOptionalsGridRowViewModel>();
            foreach (var optional in ManageOptionalsData.Rows)
                ManOptionalsVModel.Rows.Add(new ManageOptionalsGridRowViewModel
                {
                    VendorID = optional.VendorID,
                    OptionalID = optional.OptionalID,
                    IsActive = optional.Active == null ? false : Convert.ToBoolean(optional.Active),
                    VendorName = optional.VendorName,
                    City = optional.City,
                    ContractStartDate = optional.ContractStartDate.ToString("dd/MMM/yy"),
                    ContractEndDate = optional.ContractEndDate.ToString("dd/MMM/yy"),
                });
            if (ManOptionalsVModel.Rows.Count == ManageOptionalsGridBData.PageCount * PageNum)
                ManOptionalsVModel.IsCanView = true;
            else
                ManOptionalsVModel.IsCanView = false;
            return ManOptionalsVModel;
        }
        private ManageOptionalsGridViewModel InitManageOptionalsSearchParams(ManageOptionalsGridBData ManageOptionalsData)
        {
            ManageOptionalsGridViewModel ManOptionalsVModel = new ManageOptionalsGridViewModel();
            ManOptionalsVModel.CitySearchText = "";
            ManOptionalsVModel.CitySearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var city in ManageOptionalsData.City)
                ManOptionalsVModel.CitySearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = city.Value, Value = city.Value });
            ManOptionalsVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in ManageOptionalsData.Active)
                ManOptionalsVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            return ManOptionalsVModel;
        }
        private OptionalDetailsViewModel InitOptionalDetailsViewModel(OptionalDetailsBData data)
        {
            OptionalDetailsViewModel OptionalDetVModel = new OptionalDetailsViewModel
            {
                OptionalID = data.OptionalID,
                OptionalSelectedVendorID = data.SelectedVendorID,
                OptionalSelectedVendorName = data.SelectedVendorName,
                ContractStartDate = data.OptionalID != 0 ? data.ContractStartDate.ToString("dd/MMM/yy") : "",
                ContractEndDate = data.OptionalID != 0 ? data.ContractEndDate.ToString("dd/MMM/yy") : "",
                ContractSignDate = data.ContractSignDate != null ? data.ContractEndDate.ToString("dd/MMM/yy") : "",
                Active = data.Active == null ? false : Convert.ToBoolean(data.Active),
                Description = data.Description,
            };
            OptionalDetVModel.Rows = new List<OptionalDetailsGridRowViewModel>();
            foreach (var rows in data.Rows)
            {
                OptionalDetVModel.Rows.Add(new OptionalDetailsGridRowViewModel()
                {
                    OptionalDetailID = rows.OptionalDetailID,
                    PaxFrom = rows.PaxFrom.ToString(),
                    PaxTo = rows.PaxTo.ToString(),
                    SelectedCurrency = rows.SelectedCurrency == "CAD" ? 2 : 1,
                    Price = Convert.ToDecimal(rows.Price).ToString("F"),
                    BusIncluded = rows.BusIncluded == null ? false : Convert.ToBoolean(rows.BusIncluded),
                    GuideIncluded = rows.GuideIncluded == null ? false : Convert.ToBoolean(rows.GuideIncluded),

                });
            }
            OptionalDetVModel.Currency = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var curr in data.Currency)
                OptionalDetVModel.Currency.Add(new System.Web.WebPages.Html.SelectListItem() { Text = curr.Value, Value = curr.Key.ToString() });
            return OptionalDetVModel;
        }
        private ManageOptionalsGridBData InitSearchOptional(ManageOptionalsGridViewModel Data)
        {
            ManageOptionalsGridBData SrchData = new ManageOptionalsGridBData
            {
                VendorName = Data.OptionalVendorNameText,
                SelectedCity = Data.CitySearchText,
                SelectedActive = Data.IsActiveSearch,
                ContractStartDate = Data.OptionalStartDateText != null ? Convert.ToDateTime(Data.OptionalStartDateText) : (DateTime?)null,
                ContractEndDate = Data.OptionalEndDateText != null ? Convert.ToDateTime(Data.OptionalEndDateText) : (DateTime?)null,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private OptionalDetailsBData InitOptionalDetailsBData(OptionalDetailsViewModel data)
        {
            OptionalDetailsBData OptionalBData = new OptionalDetailsBData
            {
                OptionalID = data.OptionalID,
                SelectedVendorID = data.OptionalSelectedVendorID,
                ContractStartDate = Convert.ToDateTime(data.ContractStartDate),
                ContractEndDate = Convert.ToDateTime(data.ContractEndDate),
                ContractSignDate = data.ContractEndDate != null ? Convert.ToDateTime(data.ContractEndDate) : (DateTime?)null,
                Description = data.Description,
                Active = data.Active,
            };

            OptionalBData.Rows = new List<OptionalDetailsRowBData>();
            if (data.Rows != null)
            {
                foreach (var row in data.Rows)
                {
                    OptionalBData.Rows.Add(new OptionalDetailsRowBData()
                    {
                        OptionalDetailID = row.OptionalDetailID,
                        PaxFrom = Convert.ToInt16(row.PaxFrom),
                        PaxTo = Convert.ToInt16(row.PaxTo),
                        SelectedCurrency = row.SelectedCurrency == 2 ? "CAD" : "USD",
                        Price = row.Price != null ? Convert.ToDecimal(row.Price) : (decimal?)null,
                        BusIncluded = row.BusIncluded,
                        GuideIncluded = row.GuideIncluded,
                    });
                }
            }
            return OptionalBData;
        }
        #endregion
    }
}